﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Jayden.Dll.Cards.Poker;
using Jayden.Dll.Cards;
using System.Drawing.Imaging;
using Jayden.Dll.Cards.Drawing;
using Jayden.Dll.Cards.Poker.Enums;

namespace Jayden.Windows.PokerClient
{
	public partial class PokerLobbyForm : Form
	{
		public WindowsPokerClient Client;
		private System.Timers.Timer m_Timer = new System.Timers.Timer();

		public PokerLobbyForm()
		{
			InitializeComponent();

			m_Timer.Elapsed += new System.Timers.ElapsedEventHandler(RefreshLobby);
			m_Timer.Interval = 5000;
			m_Timer.Enabled = true;
		}

		private void RefreshLobby(object sender, System.Timers.ElapsedEventArgs e)
		{
			if (Visible == false || WindowState != FormWindowState.Minimized)
			{
				if (Client != null)
					Client.Lobby(PokerLimit.NoLimit, PokerType.TexasHoldem, 0, ulong.MaxValue, 0, ulong.MaxValue, "EUR|USD|POK", 0, byte.MaxValue, 0, byte.MaxValue);
			}
		}

		public void SetCashGamesList(List<PokerTableInfo> cashGames)
		{
			lvCashGames.BeginUpdate();
			lvCashGames.Items.Clear();
			foreach (PokerTableInfo info in cashGames)
			{
				PokerCurrency currency = PokerCurrency.GetCurrency(info.Currency);
				string[] columns  = new string[lvCashGames.Columns.Count];
				columns[0] = info.TableName;
				columns[1] = info.Currency;
				columns[2] = currency.ToString(info.MinimumBuyIn) + "-" + currency.ToString(info.MaximumBuyIn);
				columns[3] = currency.ToString(info.SmallBlind) + "/" + currency.ToString(info.BigBlind);
				columns[4] = info.Players + "/" + info.Seats;
				ListViewItem item = new ListViewItem(columns);
				item.Tag = info;
				lvCashGames.Items.Add(item);
			}
			lvCashGames.EndUpdate();
		}
		public void SetTournamentsList(List<PokerTournamentInfo> tournaments)
		{
			lvTournaments.BeginUpdate();
			lvTournaments.Items.Clear();
			foreach (PokerTournamentInfo info in tournaments)
			{
				PokerCurrency currency = PokerCurrency.GetCurrency(info.Currency);
				string[] columns = new string[lvTournaments.Columns.Count];
				columns[0] = info.TournamentName;
				columns[1] = info.Currency;
				columns[2] = currency.ToString(info.EntryPrice);
				columns[3] = info.StartDateTime.ToString();
				columns[4] = info.Entrants + "/" + info.MaxEntry;
				ListViewItem item = new ListViewItem(columns);
				item.Tag = info;
				lvTournaments.Items.Add(item);
			}
			lvTournaments.EndUpdate();
		}

		private void lvTables_DoubleClick(object sender, EventArgs e)
		{
			if (lvCashGames.SelectedItems.Count == 1)
			{
				string tableName = (lvCashGames.SelectedItems[0].Tag as PokerTableInfo).TableName;
				Client.Observe(tableName);
				WindowState = FormWindowState.Minimized;
			}
		}

		protected override void OnClosing(CancelEventArgs e)
		{
			base.OnClosing(e);
			if (MessageBox.Show(this, "Are you sure you want to exit the program and disconnect from the server ?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
				e.Cancel = true;
		}

		protected override void OnClosed(EventArgs e)
		{
			base.OnClosed(e);

			Application.Exit();
		}

		private void bankrollToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Client.AskBankroll();
		}

		private void avatarToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				try
				{
					string filename = openFileDialog.FileName;
					PlayerImage image = new PlayerImage(filename);
					image.TableImage.Save("table." + Client.Login + ".png", ImageFormat.Png);
					Client.SendTableImage(image);
				}
				catch (Exception exc)
				{
					MessageBox.Show(exc.Message);
				}
			}
		}

		private void colorsDeckToolStripMenuItem_Click(object sender, EventArgs e)
		{
			colorsDeckToolStripMenuItem.Checked = !colorsDeckToolStripMenuItem.Checked;
		}

		private void autoMuckLosingHandToolStripMenuItem_Click(object sender, EventArgs e)
		{
			autoMuckLosingHandToolStripMenuItem.Checked = !autoMuckLosingHandToolStripMenuItem.Checked;
		}

		public bool FourColorDeck { get { return colorsDeckToolStripMenuItem.Checked; } }
		public bool AutoMuckLosingHand { get { return autoMuckLosingHandToolStripMenuItem.Checked; } }

		private void lvTournaments_DoubleClick(object sender, EventArgs e)
		{
			if (lvTournaments.SelectedItems.Count == 1)
			{
				string tournamentName = (lvTournaments.SelectedItems[0].Tag as PokerTournamentInfo).TournamentName;
				Client.TournamentLobby(tournamentName);
				Client.TournamentLobbyPlayers(tournamentName);
				Client.TournamentLobbyBettingStructure(tournamentName);
				Client.TournamentLobbyPayoutStructure(tournamentName);
			}
		}

		private void texasHoldemToolStripMenuItem_Click(object sender, EventArgs e)
		{
			System.Diagnostics.Process.Start("https://en.wikipedia.org/wiki/Texas_hold_'em#Rules");
		}
	}
}
